<?php

/**
 * Description of ContentEmployeeLibCard
 *
 * @author Honza, Petr
 */
class ContentEmployeeLibCard implements IMainBodyWithInteraction {
  public function action() {
    //create library card and set expiration on now + 1 year
    if ($_GET['a'] == 'create') {
        // create a lib card 
        $sql = 'update ' . TBL_USERS . ' set library_card_number = ' . time() . ', library_card_validity = CURDATE() + interval 1 year where id_user=' . $_GET['ida'];
        DB::sql($sql);
        
        header('location: index.php?where=employee&subwhere=libcard');
    }

    //update information about library card -> extend data of validity on now+1year
    if ($_GET['a'] == 'extend') {
        // longer the validity by year
        $sql = 'update ' . TBL_USERS . ' set library_card_number = ' . time() . ', library_card_validity = library_card_validity + interval 1 year where id_user=' . $_GET['ida'];
        DB::sql($sql);
     
        header('location: index.php?where=employee&subwhere=libcard');
    }
  }

  public function applyForm($formID = NULL) {
    
  }

  public function getContent() {
    $content = '<h2>Library cards</h2>';
    
    $this->action();
    $content .= $this->getTable();
    return $content;
  }

  public function getForm($formID = NULL, $error = NULL) {
    
  }

  public function getSubmitedForm() {
    
  }

  public function getTable($tableID = NULL) {

    //draw table with all users
    $sql = 'select id_user, name,surname, ifnull(DATE_FORMAT(library_card_validity, \'%Y-%m-%d\'),\'No library card\') as validity from ' . TBL_USERS;
    $sql .= ' where id_role = ' . ROLE_USER_ID;
    
    
    $result = DB::sql($sql);
    
    $table = '<table>';
    $table .= '<tr>';
    $table .= '<th>Name</th><th>Surname</th><th>Library card validity</th><th>Extend/Create</th>';
    $table .= '</tr>';
    foreach ($result as &$row) {
        $userID = $row['id_user'];
        if ($row['validity'] == 'No library card'){
            $actionText = 'Create';
            $action = 'create';
        } else {
            $actionText = 'Extend';
            $action = 'extend';
        }
        $table .= '<tr>';
        $table .= '<td>'. $row['name'] .'</td>';
        $table .= '<td>'. $row['surname'] .'</td>';
        $table .= '<td>'. $row['validity'] .'</td>';
        $table .= '<td><a href="index.php?where=employee&subwhere=libcard&a='. $action .'&ida=' . $userID . '">' . $actionText .'</a></td>';
        $table .= '</tr>';
    }
    $table .= '';
    $table .= '</table>';
    
    return $table;
  }  
}

?>
